package com.xiyu.service.repository.elder.register;

import com.xiyu.service.model.elder.info.ElderInfoFetcher;
import com.xiyu.service.model.elder.register.ElderCheckOutFetcher;
import com.xiyu.service.vo.elder.checkOut.CheckOutPageQueryInput;
import com.xiyu.service.model.elder.register.ElderCheckOut;
import com.xiyu.service.model.elder.register.ElderCheckOutTable;
import org.babyfish.jimmer.spring.repository.JRepository;
import org.babyfish.jimmer.Page;
import org.springframework.util.StringUtils;
import java.util.Optional;

public interface ElderCheckOutRepository extends JRepository<ElderCheckOut, Long> {
    ElderCheckOutTable elderCheckOutTable = ElderCheckOutTable.$;

    default Page<ElderCheckOut> pageQuery(CheckOutPageQueryInput inputVO){
        return sql().createQuery(elderCheckOutTable)
                .whereIf(StringUtils.hasText(inputVO.getName()), () -> elderCheckOutTable.elder().name().like(inputVO.getName()))
                .whereIf(StringUtils.hasText(inputVO.getIdentityCard()), () -> elderCheckOutTable.elder().identityCard().like(inputVO.getIdentityCard()))
                .whereIf(inputVO.getCheckOutType() != null, elderCheckOutTable.checkOutType().eq(inputVO.getCheckOutType()))
                .orderBy(elderCheckOutTable.id().desc())
                .select(elderCheckOutTable.fetch(ElderCheckOutFetcher.$.allTableFields().elder(ElderInfoFetcher.$.allTableFields())))
                .fetchPage(inputVO.getPageNo() - 1, inputVO.getPageSize());
    }

    default Optional<ElderCheckOut> findDetailById(Long id){
        return sql().createQuery(elderCheckOutTable)
                .where(elderCheckOutTable.id().eq(id))
                .select(elderCheckOutTable.fetch(ElderCheckOutFetcher.$.allTableFields().elder(ElderInfoFetcher.$.allTableFields())))
                .fetchOptional();
    }
}